#pragma once
#include <iostream>
#include <cstdarg>
#include <ctime>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;

enum
{
    Debug = 0,
    Info,
    Warning,
    Error,
    Fatal
};

enum
{
    Screen = 10, // 写到显示器
    OneFile,     // 写到一个文件
    ClassFile    // 写到多个文件
};

const int defaultstyle = Screen;
const std::string default_filename = "log.";
const std::string logdir = "log";

class Log
{
public:
    Log()
        : style(defaultstyle),
          filename(default_filename)
    {
        mkdir(logdir.c_str(),0775);
    }

    void Enable(int sty)
    {
        style = sty;
    }

    std::string LevelToString(int level)
    {
        switch (level)
        {
        case Debug:
            return "Debug";
        case Info:
            return "Info";
        case Warning:
            return "Warning";
        case Error:
            return "Error";
        case Fatal:
            return "Fatal";
        default:
            return "Unknown";
        }
    }
    std::string TimeStampExLocalTime()
    {

        time_t currtime = time(nullptr);
        struct tm *local_time = localtime(&currtime);
        char time_buff[128];
        snprintf(time_buff, sizeof(time_buff), "%d-%d-%d %d:%d:%d",
                 local_time->tm_year + 1900, local_time->tm_mon + 1, local_time->tm_mday, local_time->tm_hour, local_time->tm_min, local_time->tm_sec);

        return time_buff;
    }

    // 写到单个文件
    void WriteLogToOneFile(const string &logname, const string &message)
    {
        umask(0);
        int fd = open(logname.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0666);
        if (fd < 0)
            return;
        write(fd, message.c_str(), message.size());
        close(fd);
        // std::ofstream out(logname);
        // if (!out.is_open())
        //     return;
        // out.write(message.c_str(), message.size());
        // out.close();
    }
    // 写到多个文件
    void WriteLogToClassFile(const string &levelstr, const string &message)
    {
        string logname = logdir;
        logname+='/';
        logname+=filename;
        logname += levelstr;

        WriteLogToOneFile(logname, message);
    }

    //
    void WriteLog(const string &levelstr, const std::string &message)
    {
        switch (style)
        {
        case Screen:
            cout << message << endl;
            break;
        case OneFile:
            WriteLogToOneFile("all", message);
            break;
        case ClassFile:
            WriteLogToClassFile(levelstr, message);
            break;
        }
    }

    // 日志信息
    void LogMessage(int level, const char *format, ...)
    {
        char rightbuffer[1024];
        // 指针
        va_list args;

        va_start(args, format);
        vsnprintf(rightbuffer, sizeof(rightbuffer), format, args);

        va_end(args);

        char leftbuffer[1024];
        std::string currtime = TimeStampExLocalTime(); // 时间
        std::string levelstr = LevelToString(level);   // 级别
        std::string idstr = std::to_string(getpid());  // id
        snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%s][%s] ", levelstr.c_str(), currtime.c_str(), idstr.c_str());
        // printf("%s%s\n",leftbuffer,rightbuffer);

        std::string loginfo = leftbuffer;
        loginfo += rightbuffer;
        WriteLog(levelstr, loginfo);
    }

    ~Log()
    {
    }

private:
    int style;
    std::string filename;
};

Log lg;

class Conf
{
public:
    Conf()
    {
        lg.Enable(Screen);
    }
    ~Conf()
    {}       
};
Conf conf;